package com.bill.common.system.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;









import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.bill.common.system.dao.ITransactionRecordDAO;
import com.bill.common.system.dto.SingleGroupReportStatDTO;
import com.bill.common.system.dto.TransactionRecordDTO;
import com.bill.common.system.entity.TransactionRecord;
import com.bill.common.user.dto.RegisterUserGridDTO;
import com.bill.common.utils.DoubleFormat;
import com.sinba.common.dto.PageDTO;

public class TransactionRecordService implements ITransactionRecordService {
	@Autowired
	private ITransactionRecordDAO transactionRecordDAO;
	//分页
	@Transactional
	public PageDTO findTransactionRecordPager(TransactionRecordDTO qdto,String sord, String sidx){
		if("bonus".equals(qdto.getSearchType())){
			qdto.setTransactionTypes(new String[]{"销售佣金","销售管理佣金","财富见点佣金","财富见点管理佣金","成员倒钩佣金","运营中心","股票交易佣金 ","静态股票收益"});
		}
		PageDTO p = transactionRecordDAO.findTransactionRecordPager(qdto, sord, sidx);
		List<TransactionRecord> trs = (List<TransactionRecord>)p.getData();
		
		List<TransactionRecordDTO> dtos = new ArrayList<TransactionRecordDTO>();
		
		for(TransactionRecord entity:trs){
			TransactionRecordDTO dto = new TransactionRecordDTO();
			dto.entity2dto(entity, dto);
			dtos.add(dto);
		}
		p.setData(dtos);
		return p;
	}
	@Transactional
	public void saveTrans(TransactionRecordDTO dto){
		TransactionRecord entity = new TransactionRecord();
		dto.dto2entity(dto, entity);
		transactionRecordDAO.save(entity);
	}
	@Transactional
	public void deleteTrans(Long[] ids){
		transactionRecordDAO.deleteByIds(ids);
	}
	
	@Transactional
	public Double statAward(TransactionRecordDTO dto){
		Double total = 0D;
		List<TransactionRecord> trs = transactionRecordDAO.findTransactionRecord(dto);
		for(TransactionRecord tr:trs){
			total=total+tr.getSumOfMoney();
		}
		return total;
	}
	
	@Transactional
	public Map<String, Double> transacGroupReportStat(
			TransactionRecordDTO qdto) {
		// TODO Auto-generated method stub
		List<SingleGroupReportStatDTO> groupData = transactionRecordDAO.transacGroupReportStat(qdto);
		Map<String, Double> map = new HashMap<String, Double>();
		map.put("销售佣金", 0D);
		map.put("销售管理佣金", 0D);
		map.put("财富见点佣金", 0D);
		map.put("财富见点管理佣金", 0D);
		map.put("成员倒钩佣金", 0D);
		map.put("运营中心", 0D);
		map.put("股票交易佣金", 0D);
		map.put("静态股票收益", 0D);
		Double sum = 0D;
		for(SingleGroupReportStatDTO dto : groupData){
			map.put(dto.getGroupField().toString(), DoubleFormat.format(dto.getAggField()));
			sum=sum+dto.getAggField();
		}
		map.put("总计", sum);
		return map;
		
	}
	
	@Transactional
	public List<SingleGroupReportStatDTO> statRecharge(TransactionRecordDTO dto){
		List<SingleGroupReportStatDTO> groupData = transactionRecordDAO.statRecharge(dto);
		return groupData;
	}
	
	//查询钱包余额
	@Transactional
	public Double findUserBalance(String loginName , String dtype ){
		List<TransactionRecord> trs = transactionRecordDAO.findUserBalance(loginName, dtype);
		if(trs!=null && trs.size()>0){
			return trs.get(0).getBalance();
		}else
			return 0D;
	}
	/**
	 * 统计各账户收入、支出统计
	 */
	@Transactional
	public List<SingleGroupReportStatDTO> statAccount(TransactionRecordDTO dto){
		return transactionRecordDAO.statAccount(dto);
	};
	
	/**
	 * 获取最新一条极差奖金记录
	 */
	@Transactional
	public TransactionRecord findLastJiChaRecord(String loginName,String transactionType){
		List<TransactionRecord> trs = transactionRecordDAO.findTransactionRecord(loginName, transactionType);
		if(trs.size()>0){
			return trs.get(0);
		}else{
			return null;
		}
	}
	
	
	/**
	 * 统计静态、动态奖金
	 */
	@Transactional
	public List<SingleGroupReportStatDTO> statDtype(TransactionRecordDTO dto){
		return transactionRecordDAO.statDtype(dto);
	};
	
	/**
	 * 更新交易记录
	 * @param tr
	 * @param dtype
	 * @param moeny
	 */
	@Transactional
	public synchronized void updateTran(TransactionRecord tr , String dtype, Double moeny ,String opttype){
		//得到全额
		Double balance = findUserBalance(tr.getLoginName(), dtype);
		if("ADD".equals(opttype)){
			tr.setBalance(DoubleFormat.format(balance+moeny));
			tr.setIncome(DoubleFormat.format(moeny));
		}else if("CUT".equals(opttype)){
			tr.setBalance(DoubleFormat.format(balance-moeny));
			tr.setExpenditure(DoubleFormat.format(moeny));
		}
		tr.setOptDate(new Date());
		tr.setDtype(dtype);
		transactionRecordDAO.save(tr);
	}
	public RegisterUserGridDTO subAccountBonus( RegisterUserGridDTO registerUserGridDTO){
		String sidx = "optDate";
		String sord="desc";
		TransactionRecordDTO trDTO = new TransactionRecordDTO();
		trDTO.setLoginName( registerUserGridDTO.getLoginName() );
		trDTO.setStart( 0 );trDTO.setLimit( 1 );
		trDTO.setSearchType("Static");
		trDTO.setDtype("Static");
		List<TransactionRecordDTO> list = findTransactionRecordPager(trDTO, sord, sidx).getData();
		Double day = 0D;
		if (list != null && list.size() > 0 ) {
			TransactionRecordDTO recordDTO = list.get(0);
			day = recordDTO.getIncome();
		}
		Double expenditure = 0D;
		TransactionRecordDTO trDto2 = new TransactionRecordDTO();
		trDto2.setInOrExp("expenditure");
		trDto2.setDtype("Static");
		trDto2.setLoginName( registerUserGridDTO.getLoginName() );
		List<SingleGroupReportStatDTO> sgrs2 = statAccount(trDto2);
		for(SingleGroupReportStatDTO sgr : sgrs2){
			if(sgr.getAggField()!=null){
				expenditure = expenditure+sgr.getAggField();
			}
		}
		Double income = 0D;
		TransactionRecordDTO trDto1 = new TransactionRecordDTO();
		trDto1.setInOrExp("income");
		trDto1.setDtype("Static");
		trDto1.setLoginName(registerUserGridDTO.getLoginName());
		List<SingleGroupReportStatDTO> sgrs = statAccount(trDto1);
		for(SingleGroupReportStatDTO sgr : sgrs){
			if(sgr.getAggField()!=null){
				income = income+sgr.getAggField();
			}
		}
		registerUserGridDTO.setDayFenHong( day );
		registerUserGridDTO.setAllFenHong( income - expenditure );
		return registerUserGridDTO;
	}
	
	@Transactional
	public Double statStatic(String loginName){

		Double result = 0D;
		try {
			List<Object> objs = transactionRecordDAO.statStatic(loginName);
			
			for(Object obj : objs){
				result = (Double)obj;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return result;
	}
	@Transactional
	public Double statStatic(String loginName,String remark){

		Double result = 0D;
		try {
			List<Object> objs = transactionRecordDAO.statStatic(loginName,remark);
			
			for(Object obj : objs){
				result = (Double)obj;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		return result;
	}
	
	@Transactional
	public List<Object[]> statDynamicAccountBalance(){
		return transactionRecordDAO.statDynamicAccountBalance();
	}
	
	
	@Transactional
	public Double statTotalAward(String loginName){

		Double result = 0D;
		try {
			List<Object> objs = transactionRecordDAO.statTotalAward(loginName);
			for(Object obj : objs){
				result = (Double)obj;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	
	
	
	
}
